package com.hrm.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hrm.config.MsgType;
import com.hrm.entity.Message;
import com.hrm.entity.UserLog;
import com.hrm.service.UserLogService;
import com.hrm.util.DateUtil;
import com.hrm.util.JSONUtil;
import com.hrm.util.Page;


@Controller
@RequestMapping("/userLog.do")
public class UserLogAction {
	private static final Log log = LogFactory.getLog(UserLogAction.class); 
	@Autowired
	private UserLogService userLogService;
	public UserLogService getUserLogService() {
		return userLogService;
	}
	public void setUserLogService(UserLogService userLogService) {
		this.userLogService = userLogService;
	}
	@RequestMapping(params = "method=getUserLogs")
	public String getUserLogs(HttpServletRequest request,
			HttpServletResponse response, ModelMap map){
		//获取所有数据的前15条数据
		Page page = new Page();
		page.setPagesize(Integer.parseInt(request.getParameter("rows")));
		page.setCurrentpage(Integer.parseInt(request.getParameter("page")));
		Map<String, Object> searchMap = new HashMap<String, Object>();
		if("true".equals(request.getParameter("_search"))){
			searchMap.put(request.getParameter("searchField"),request.getParameter("searchString"));
		}
		List<UserLog> userLogs = userLogService.getUserLogs(page);
		page.setCounts(userLogService.getUserLogCounts());
		if(userLogs!=null){
			log.info("begin to getUserLogs");
			try {
				Map<String, Object> resultMap2 = new HashMap<String, Object>();
				resultMap2.put("page", page.getCurrentpage()+"");
				resultMap2.put("total", page.getCountPage()+"");
				resultMap2.put("records", page.getCounts()+"");
				List<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();
				for(UserLog u:userLogs){
					Map<String, Object> rowMap =new HashMap<String, Object>();
					rowMap.put("id", u.getId());
					List<String> list = new ArrayList<String>();
					list.add(u.getId()+"");
					//配置实现表格超链接
					list.add(u.getUserId()+"");
					list.add(DateUtil.formate(u.getOperationTime())+"");
					list.add(u.getMsgType()+"");
					list.add(u.getMsg());
					list.add(u.getUserIp());
					rowMap.put("cell", list);
					rows.add(rowMap);
				}
				resultMap2.put("rows", rows);
				String result=JSONUtil.parseMap(resultMap2);
				response.getWriter().print(result);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else{
			try {
				Message message = new Message("ERROR", "获取数据失败或数据为空!", new Date());
				response.getWriter().print(message.toJSONString());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
		
	}
	@RequestMapping(params = "method=deleteUserLogById")
	public String deleteUserLogById(HttpServletRequest request,
			HttpServletResponse response, ModelMap map){
		String userLogids = request.getParameter("userLogids")==null?"":request.getParameter("userLogids");
		Message message = new Message();
		if("".equals(userLogids)){
			message.setMsg("必须传入用户日志ID才能删除!");
			message.setStatus("ERROR");
			message.setTimestamp(new Date());
			try {
				response.getWriter().print(message.toJSONString());
				return null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if(userLogService.deleteUserLogById(userLogids)<1){
			message.setMsg("网络忙，请稍后再试!");
			message.setStatus("ERROR");
			message.setTimestamp(new Date());
			try {
				response.getWriter().print(message.toJSONString());
				return null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else{
			message.setMsg("删除用户日志成功!");
			message.setStatus("SUCCESS");
			message.setTimestamp(new Date());
			ActionUtil.insertLog(request, MsgType.TYPE_USERLOG, "删除用户日志:删除的用户日志Id为"+userLogids,userLogService);
			try {
				response.getWriter().print(message.toJSONString());
				return null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
		
	}
}
